﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_Links.cs
* 创建标识：曾乃强 15158756652 2011-03-17 11:03:01
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_Links*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_Links
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_Links obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "Links ([LinkName],[LinkAddress],[LinkPic],[LinkType],[OrderID],[LinkClassID])");
            sql.Append("values(@LinkName,@LinkAddress,@LinkPic,@LinkType,@OrderID,@LinkClassID)");
            
            SqlParameter[] pra = {
                            new SqlParameter("@LinkName",obj.LinkName),
							new SqlParameter("@LinkAddress",obj.LinkAddress),
							new SqlParameter("@LinkPic",obj.LinkPic),
							new SqlParameter("@LinkType",obj.LinkType),
							new SqlParameter("@OrderID",obj.OrderID),
							new SqlParameter("@LinkClassID",obj.LinkClassID)
                                 };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Update(Model.CNVP_Links obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "Links set [LinkName] = @LinkName,[LinkAddress] = @LinkAddress,[LinkPic] = @LinkPic,[LinkType] = @LinkType,[OrderID] = @OrderID,[LinkClassID] = @LinkClassID where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@LinkName",obj.LinkName),
							new SqlParameter("@LinkAddress",obj.LinkAddress),
							new SqlParameter("@LinkPic",obj.LinkPic),
							new SqlParameter("@LinkType",obj.LinkType),
							new SqlParameter("@OrderID",obj.OrderID),
							new SqlParameter("@LinkClassID",obj.LinkClassID)
                                };
             return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public int Delete(Model.CNVP_Links obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "Links where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }

        public List<Model.CNVP_Links> LoadAll(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[LinkName],[LinkAddress],[LinkPic],[LinkType],[OrderID],[LinkClassID] from " + Prefix + "Links");
            if (sqlWhere.Trim() != "")
            {
                sql.Append(" where " + sqlWhere);
            }
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_Links> list=new List<Model.CNVP_Links>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_Links cnvp_links = new Model.CNVP_Links();
                    cnvp_links.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_links.LinkName = dr["LinkName"] is DBNull ? String.Empty : (string)dr["LinkName"];
                    cnvp_links.LinkAddress = dr["LinkAddress"] is DBNull ? String.Empty : (string)dr["LinkAddress"];
                    cnvp_links.LinkPic = dr["LinkPic"] is DBNull ? String.Empty : (string)dr["LinkPic"];
                    cnvp_links.LinkType = dr["LinkType"] is DBNull ? 0 : (int)dr["LinkType"];
                    cnvp_links.OrderID = dr["OrderID"] is DBNull ? 0 : (int)dr["OrderID"];
                    cnvp_links.LinkClassID = dr["LinkClassID"] is DBNull ? 0 : (int)dr["LinkClassID"];
                    list.Add(cnvp_links);
                }
            }
            return list;
        }


        public Model.CNVP_Links LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [LinkName],[LinkAddress],[LinkPic],[LinkType],[OrderID],[LinkClassID] from " + Prefix + "Links");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra); 
            Model.CNVP_Links cnvp_links = new Model.CNVP_Links();
            if (DT != null && DT.Rows.Count > 0)
            {
               
                DataRow dr = DT.Rows[0];
                cnvp_links.LinkName = dr["LinkName"] is DBNull ? String.Empty : (string)dr["LinkName"];
                cnvp_links.LinkAddress = dr["LinkAddress"] is DBNull ? String.Empty : (string)dr["LinkAddress"];
                cnvp_links.LinkPic = dr["LinkPic"] is DBNull ? String.Empty : (string)dr["LinkPic"];
                cnvp_links.LinkType = dr["LinkType"] is DBNull ? 0 : (int)dr["LinkType"];
                cnvp_links.OrderID = dr["OrderID"] is DBNull ? 0 : (int)dr["OrderID"];
                cnvp_links.LinkClassID = dr["LinkClassID"] is DBNull ? 0 : (int)dr["LinkClassID"];
            }
            return cnvp_links;
        }

        #endregion

        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string AllFields = "a.ID, a.LinkName, a.LinkAddress, a.LinkType, a.OrderID,b.ColumnName";
                string Condition = "" + Prefix + "Links as a left join " + Prefix + "Column as b on a.LinkClassID= b.ID";
                string IndexField = "a.ID";
                string OrderFields = "order by a.OrderID, a.id desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }
    }
}

